#include "httphelper.h"

HttpHelper::HttpHelper() {
    curl_global_init(CURL_GLOBAL_DEFAULT);
}
HttpHelper::~HttpHelper() {
    curl_global_cleanup();
}
curl_slist* HttpHelper::getHeader() {
    curl_slist* headers = NULL;
    //
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36");
    headers = curl_slist_append(headers, "X-Request-ID: 12345");
    return headers;
}
std::string HttpHelper::get(const std::string& url) {
    auto curl = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>(
        curl_easy_init(), &curl_easy_cleanup);

    if (!curl) throw std::runtime_error("CURL init failed");

    std::string response;
    curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, getHeader());
    curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response);

    if (curl_easy_perform(curl.get()) != CURLE_OK) {
        throw std::runtime_error("Request failed");
    }
    return response;

}

std::string HttpHelper::post(const std::string& url, const std::string& data) {
    auto curl = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>(
        curl_easy_init(), &curl_easy_cleanup);

    if (!curl) throw std::runtime_error("CURL init failed");

    std::string response;
    curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, getHeader());
    curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, data.c_str());
    curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 0L);

    curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response);

    if (curl_easy_perform(curl.get()) != CURLE_OK) {
        throw std::runtime_error("Request failed");
    }
    return response;
}


std::string HttpHelper::put(const std::string& url) {

    auto curl = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>(
        curl_easy_init(), &curl_easy_cleanup);

    if (!curl) throw std::runtime_error("CURL init failed");

    std::string response;
    curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, getHeader());
    curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response);

    if (curl_easy_perform(curl.get()) != CURLE_OK) {
        throw std::runtime_error("Request failed");
    }
    return response;
}

std::string HttpHelper::header(const std::string& url) {

    auto curl = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>(
        curl_easy_init(), &curl_easy_cleanup);

    if (!curl) throw std::runtime_error("CURL init failed");

    std::string response;
    curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, getHeader());
    curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response);

    if (curl_easy_perform(curl.get()) != CURLE_OK) {
        throw std::runtime_error("Request failed");
    }
    return response;
}

size_t HttpHelper::write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
    auto& buffer = *static_cast<std::string*>(userdata);
    buffer.append(ptr, size * nmemb);
    return size * nmemb;
};

/***
  try {
        CurlClient client;
        auto response = client.post(
            "https://example.com/api",
            R"({"key":"value"})"
        );
        std::cout << response << std::endl;
    } catch(const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return 0;
*/
